package com.ruoyi.ic.util;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.RConst;
import com.ruoyi.ic.mapper.IcGeneralHMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/* 单据号生成
* */
@Component
public class DocumentNum {
    @Autowired
    private IcGeneralHMapper icGeneralHMapper;

    public String getVbillcode(Date date){
        // 获取日期格式化yyyyMMdd
        String vbillcode = new SimpleDateFormat(Constants.DATEFORMATTING).format(date);
        // yyyyMMdd + 从数据库获取最近的
        String num = icGeneralHMapper.selectLikeData(vbillcode+"%");
        // 为null从001开始
        if(num==null){
            return vbillcode + Constants.NUM;
        }else{
            num = num.substring(num.length() - RConst.Num.INT_3);
            return vbillcode + String.format("%03d",Integer.parseInt(num)+1);
        }
    }

}